package com.example.devices

import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.app.path.RouterPath
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.databinding.ActivityOrderDetailBinding
import com.example.devices.vm.DevicesViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_ORDER_DETAIL)
class OrderDetailActivity : BaseMvvmActivity<ActivityOrderDetailBinding>() {
    @Autowired
    @JvmField
    var id:String = ""
    var name:String = ""
    private val orderDetailViewModel:DevicesViewModel by viewModels()
    override fun initView() {
        ARouter.getInstance().inject(this)
        orderDetailViewModel.orderDetail(id.toInt())
        mDataBinding.btn1.setOnClickListener {
            mDataBinding.btn2.visibility = View.VISIBLE
            mDataBinding.btn3.visibility = View.VISIBLE
            mDataBinding.btn1.visibility =View.GONE
        }
        //开始巡检
        mDataBinding.btn2.setOnClickListener {
            orderDetailViewModel.inspection(id.toInt())
            orderDetailViewModel.mOrderDetailSuccess.observe(this){
                ToastUtils.showShort("开始巡检成功")
            }
            orderDetailViewModel.mError.observe(this){
                ToastUtils.showShort(it)
            }
        }
        //转派
        mDataBinding.btn3.setOnClickListener {
            orderDetailViewModel.transfer(id.toInt(),name)
            orderDetailViewModel.mInspectionSuccess.observe(this){
                ToastUtils.showShort("转派成功")
                ARouter.getInstance().build(RouterPath.PATH_DEVICES_ORDER).navigation()
            }
            orderDetailViewModel.mError.observe(this){
                ToastUtils.showShort(it)
            }
        }
    }

    override fun loadData() {
        orderDetailViewModel.mOrderDetailSuccess.observe(this){
            mDataBinding.tvName.text = it.orderType
            mDataBinding.tvStatus.text = it.status
            mDataBinding.tvTime.text = it.appointmentTime.substring(0,10)
            if(it.status == "进行中"){
                mDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_item_devices)
                mDataBinding.tvStatus.text = "已分配"
                mDataBinding.btn2.visibility = View.VISIBLE
                mDataBinding.btn3.visibility = View.VISIBLE
            }else if(it.status == "已完成"){
                mDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_item_devices_gray)
                mDataBinding.tvStatus.text = "已过期"
            }else if(it.status == "已转派"){
                mDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_item_devices_green)
                mDataBinding.tvStatus.text = "已转派"
            }else{
                mDataBinding.tvStatus.setBackgroundResource(com.example.resource.R.drawable.bg_order_red)
                mDataBinding.tvStatus.text = "未分配"
                mDataBinding.btn1.visibility = View.VISIBLE
            }
        }
        orderDetailViewModel.mError.observe(this){
            ToastUtils.showShort(it)
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_order_detail
}